package org.kjtc.balanceController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.balanceEntity.PowerFactor;
import org.kjtc.balanceService.PowerFactorService;
import org.kjtc.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PowerFactorController extends BaseController{

	@Resource(name = "powerFactorService")
	private PowerFactorService powerFactorService;

	@RequestMapping(value = "/searchPowerFactor")
	@ResponseBody
	public Map<String, Object> searchPowerFactor(PowerFactor powerFactor){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "功率因数查询开始");

		List<PowerFactor> list = null;
		try {
			list = powerFactorService.getPowerFactor(powerFactor);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "功率因数查询异常" + e.getMessage());
			return map;
		}
		map.put("powerFactorList", list);
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "功率因数查询结束");
		powerFactor = null;
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value = "/savePowerFactor")
	public Map<String, Object> savePowerFactor(PowerFactor powerFactor){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "功率因数添加开始");
		int count = 0;
		try {
			count = powerFactorService.insertPowerFactor(powerFactor);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		}  catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "功率因数添加异常");
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "功率因数添加结束");
		powerFactor = null;
		return map;
	}
	
	@RequestMapping(value = "/updatePowerFactor")
	@ResponseBody
	public Map<String, Object> updatePowerFactor(PowerFactor powerFactor) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "功率因数修改开始");
		int count = 0;
		try {
			count = powerFactorService.updatePowerFactor(powerFactor);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "功率因数修改异常");
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "功率因数修改结束");
		powerFactor = null;
		return map;
	}
	
	@RequestMapping(value = "/deletePowerFactor")
	@ResponseBody
	public Object deletePowerFactor(String ID) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "功率因数删除开始");
		PowerFactor powerFactor = new PowerFactor();
		int count = 0;
		try {
			powerFactor.setID(Integer.parseInt(ID));
			count = powerFactorService.deletePowerFactor(powerFactor);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "功率因数删除异常:" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "功率因数删除结束");
		powerFactor = null;
		return map;
	}
}
